package com.hanscan.util.export;

import com.icesoft.faces.context.effects.JavascriptContext;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;


public class ExcelUtil
{
    private String sheet;
    private List<String> listData;
    private int tittleData;
    private int columns;
    private String filename;

    public ExcelUtil()
    {
    }

    public ExcelUtil(String sheet,
            List<String> listData, int tittleData, int columns, String filename)
    {
        this.sheet = sheet;
        this.listData = listData;
        this.tittleData = tittleData;
        this.columns = columns;
        this.filename = filename;
    }


    public void generateExcel()
    {
        //Get the request from the FacesContext
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();

        //Need the base URL for the webapp, so
        String location = request.getRequestURL().toString();
        //if your webapp name is bob
        location = location.substring(0, location.indexOf("/MainMenu.iface"));

        //Get the real path to the webapp
        String realPath = ((ServletContext) context.getExternalContext().getContext()).getRealPath(
                "/");
        try
        {
            //Create the workbook
            WritableWorkbook workbook = Workbook.createWorkbook(new File(
                    realPath + filename));

            // Code to populate the workbook here
            // write, close and redirect
            WritableSheet writableSheet = workbook.createSheet(
                    sheet, 0);

            for (int i=0; i<tittleData; i++)
            {
                Label title = new Label(i, 0, listData.get(i) + ": " +
                    listData.get(i+tittleData));
                 writableSheet.addCell(title);
            }
            

            int row = 2;
            int contadorElementos = tittleData*2;
            while (contadorElementos < listData.size())
            {
                row++;
                for (int column = 0; column < columns; column++)
                {
                    if (contadorElementos < listData.size())
                    {
                        Label label = new Label(column, row, listData.get(
                                contadorElementos));
                        writableSheet.addCell(label);
                        contadorElementos++;
                    }
                }
            }

            workbook.write();
            workbook.close();
            String url = location + "/" + filename;
            JavascriptContext.addJavascriptCall(
                    FacesContext.getCurrentInstance(),
                    "window.open('" + url + "', 'myWindow');");
//            context.getExternalContext().redirect(location + "/" + filename);

        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
        catch (WriteException ex)
        {
            ex.printStackTrace();
        }
    }

    public int getColumnsName()
    {
        return columns;
    }

    public void setColumnsName(int columnsName)
    {
        this.columns = columnsName;
    }

    public List<String> getListData()
    {
        return listData;
    }

    public void setListData(List<String> listData)
    {
        this.listData = listData;
    }

    public String getSheet()
    {
        return sheet;
    }

    public void setSheet(String sheet)
    {
        this.sheet = sheet;
    }

    public int getTittleData()
    {
        return tittleData;
    }

    public void setTittleData(int tittleData)
    {
        this.tittleData = tittleData;
    }

    
}
